/**
 * 基本数据类型
 */
#pragma once

#include <stdbool.h>
#include <time.h>

/* 自定义端口号 */
#define N_OBJ_PORT 9611

/* DEBUG 开关 */
#define EVENT_DEBUG true

/* 基本类型定义 */
typedef void *epointer;
#define EPTR(any) ((epointer)(any))

typedef unsigned char  euchar;
typedef unsigned char  euint8;
typedef char	       echar;
typedef char	       eint8;
typedef unsigned short euint16;
typedef short	       eint16;
typedef unsigned int   euint32;
typedef int	       eint32;
typedef unsigned long  euint64;
typedef long	       eint64;
typedef float	       efloat32;
typedef double	       efloat64;

typedef enum
{
	EVENT_TYPE_NONE = 0,
	/* 基本数据类型 */
	EVENT_TYPE_INT8	   = 1,
	EVENT_TYPE_UINT8   = 2,
	EVENT_TYPE_INT16   = 3,
	EVENT_TYPE_UINT16  = 4,
	EVENT_TYPE_INT32   = 5,
	EVENT_TYPE_UINT32  = 6,
	EVENT_TYPE_INT64   = 7,
	EVENT_TYPE_UINT64  = 8,
	EVENT_TYPE_FLOAT32 = 9,
	EVENT_TYPE_FLOAT64 = 10,
	EVENT_TYPE_POINTER = 11,
	/* 组合数据类型 */
	EVENT_TYPE_STRING     = 21,
	EVENT_TYPE_RAW_STRUCT = 22,
	EVENT_TYPE_RAW_DATA   = 23,
	/* List 类型 */
	EVENT_TYPE_LIST = 31,

	/* 结束标志 */
	EVENT_TYPE_END = 'L',

} EventType;

/* 拓展宏、工具宏 */

/* 创建一块 Type 类型的内存 */
#define MALLOC_NEW(Type) ((Type *)malloc(sizeof(Type)))

/* 若表达式 expr 为 false，则在函数中返回 */
#define return_if_fail(expr, object_func)                                   \
	do {                                                                \
		if (!(expr)) {                                              \
			fprintf(stderr,                                     \
				"Function '" #object_func "' check failed " \
				"(%d): " #expr "\n",                        \
				__LINE__);                                  \
			return;                                             \
		}                                                           \
	} while (0)

/* 若表达式 expr 为 false，则在函数中返回 val */
#define return_val_if_fail(expr, object_func, val)                          \
	do {                                                                \
		if (!(expr)) {                                              \
			fprintf(stderr,                                     \
				"Function '" #object_func "' check failed " \
				"(%d): " #expr "\n",                        \
				__LINE__);                                  \
			return (val);                                       \
		}                                                           \
	} while (0)

/* 输出 debug 信息 */
#define event_debug(Object, function, debug_message, ...)                      \
	do {                                                                   \
		time_t __time_dbg;                                             \
		time(&__time_dbg);                                             \
		struct tm *__time_dbg_struct = localtime(&__time_dbg);         \
		fprintf(stderr,                                                \
			"\033[32m%d-%02d-%02d "                                \
			"%02d:%02d:%02d\033[0m Object<\033[01;36m" #Object     \
			"\033[0m>, Function<\033[01;33m" #function             \
			"\033[0m> (%d): " debug_message "\n",                  \
			__time_dbg_struct->tm_year + 1900,                     \
			__time_dbg_struct->tm_mon + 1,                         \
			__time_dbg_struct->tm_mday,                            \
			__time_dbg_struct->tm_hour, __time_dbg_struct->tm_min, \
			__time_dbg_struct->tm_sec, __LINE__, ##__VA_ARGS__);   \
	} while (0)
